package com.max.handler;

import com.alibaba.fastjson2.JSON;
import com.max.common.R;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


public class AuthErrorHandler implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        R<?> r = new R<>();
        r.setCode(HttpStatus.UNAUTHORIZED.value());
        r.setMsg("认证失败");
        response.setStatus(HttpStatus.OK.value());
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json;utf-8");
        response.getWriter().write(JSON.toJSONString(r));
    }
}
